package com.huc.rest.check;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.huc.rest.vo.ApiConfigVO;
import com.huc.rest.vo.ServiceCoreVO;

import javax.servlet.http.HttpServletRequest;

public class HttpContentChecks {

    private ServiceCoreVO serviceCoreVO;
    private ApiConfigVO configVO;
    private HttpServletRequest request;
    private boolean upFlag;

    public static HttpContentChecks getInstance(ServiceCoreVO vo,
                                                HttpServletRequest request) {
        HttpContentChecks utils = new HttpContentChecks();
        utils.serviceCoreVO = vo;
        utils.request = request;
        return utils;
    }

    // api内容校验
    public ApiConfigVO checkContent() {
        //校验action
        checkaction();
        //设置请求数据
        setRequestContent();
        return configVO;
    }

    private void checkaction() {
        configVO = new ApiConfigVO();
        String[] beanAndMethod = serviceCoreVO.getAction().split("_");
        configVO.setBean(beanAndMethod[0]);
        configVO.setMethod(beanAndMethod[1]);
    }

    //设置请求数据
    private void setRequestContent() {
        Object reqobject = null;
            try {
                JSONObject jsonObject = JSONObject.parseObject(serviceCoreVO.getReqContent()).getJSONObject("request");
                reqobject = jsonObject.getJSONObject("content");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        serviceCoreVO.setReqObject(reqobject);
    }
}
